/*********************************************************************
 *                                                                   *
 *  Js Css Minifier ( JsCssMin )                                     *
 *  Author: Igor Kanshyn                                             *
 *  Copyright (c) 2009, Igor Kanshyn. All rights reserved.           *
 *                                                                   *
 *  JsCssMin is released under the Apache Software License.          *
 *  See LICENSE.txt for more details.                                *
 *                                                                   *
 *********************************************************************/

package org.grenader.jscssmin;

import org.grenader.jscssmin.files.FileUtils;
import org.grenader.jscssmin.model.ResourceBlock;
import org.grenader.jscssmin.model.ResourceGroup;
import org.grenader.jscssmin.model.ResourceType;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.IOException;

/**
 * <p>Product: JsCssMin ( JavaScript and CSS Minifier )<br>
 * Author: Igor Kanshyn (grenader).
 * </p>
 * Date: Apr 25, 2009
 */
public class JCSSMinFileTestCase extends FileTestCaseBase {
    private JsCssMinConfig config;
    private String initialText = "111\n" +
            "<!-- JsCssMin Package Start -->\n" +
            "<link rel=\"stylesheet\" href=\"my1.css\" type=\"text/css\" />\n" +
            "<script src=\"my2.js\" type=\"text/javascript\"></script>\n" +
            "<link rel=\"stylesheet\" href=\"my2.css\" type=\"text/css\" />\n" +
            "<script src=\"my1.js\" type=\"text/javascript\"></script>\n" +
            "<!-- JsCssMin Package End -->\n" +
            "222";
    private File htmlFile;


    @Before
    public void setUp() throws IOException {
        super.setUp();

        config = new JsCssMinConfig();
        config.setVersion("5");
        config.setCurrentDirAbsolutePath(tmpOriginalDir.getAbsolutePath());
        config.setDestDirAbsolutePath(tmpDestinatonDir.getAbsolutePath());

        htmlFile = new File(tmpOriginalDir.getAbsolutePath(), "my.html");
        FileUtils.writeContentIntoFile(htmlFile, initialText);

    }

    @Test
    public void testCopyResourceFiles() {
        config.setVersion("5");
        JsCssMin jscssMin = new JsCssMin(initialText, new JsCssMinProcessor(config));

        jscssMin.process();
        String resText = jscssMin.getContent();
        assertEquals("111\n" +
                "<!-- JsCssMin Package Start -->\n" +
                "<link type=\"text/css\" rel=\"stylesheet\" href=\"my1_my2_v5.css\"/>\n" +
                "<script type=\"text/javascript\" src=\"my2_my1_v5.js\"></script>\n" +
                "<!-- JsCssMin Package End -->\n" +
                "222\n", resText);

        ResourceBlock block = jscssMin.getBlock(2);
        ResourceGroup cssGroup = block.getTypesMap().get(ResourceType.CSS);
        File cssResultFile = new File(tmpDestinatonDir, cssGroup.getGroupFileName(config.getVersion()));
        System.out.println("resultFile.getAbsolutePath() = " + cssResultFile.getAbsolutePath());
        assertTrue(cssResultFile.exists());

        ResourceGroup jsGroup = block.getTypesMap().get(ResourceType.JavaScript);
        File jsResultFile = new File(tmpDestinatonDir, jsGroup.getGroupFileName(config.getVersion()));
        System.out.println("resultFile.getAbsolutePath() = " + jsResultFile.getAbsolutePath());
        assertTrue(jsResultFile.exists());

    }

    @Test
    public void testProcess() {
        JsCssMinProcessor processor = new JsCssMinProcessor(config);
        config.setVersion("5");
        JsCssMin jscssMin = new JsCssMin(htmlFile, "UTF-8", processor);

        jscssMin.process();

        String resText = jscssMin.getContent();
        assertEquals("111\n" +
                "<!-- JsCssMin Package Start -->\n" +
                "<link type=\"text/css\" rel=\"stylesheet\" href=\"my1_my2_v5.css\"/>\n" +
                "<script type=\"text/javascript\" src=\"my2_my1_v5.js\"></script>\n" +
                "<!-- JsCssMin Package End -->\n" +
                "222\n", resText);

        ResourceBlock block = jscssMin.getBlock(2);
        ResourceGroup cssGroup = block.getTypesMap().get(ResourceType.CSS);
        File cssResultFile = new File(tmpDestinatonDir, cssGroup.getGroupFileName(config.getVersion()));
        System.out.println("resultFile.getAbsolutePath() = " + cssResultFile.getAbsolutePath());
        assertTrue(cssResultFile.exists());

        ResourceGroup jsGroup = block.getTypesMap().get(ResourceType.JavaScript);
        File jsResultFile = new File(tmpDestinatonDir, jsGroup.getGroupFileName(config.getVersion()));
        System.out.println("resultFile.getAbsolutePath() = " + jsResultFile.getAbsolutePath());
        assertTrue(jsResultFile.exists());

        File htmlResultFile = new File(tmpDestinatonDir, "my.html");
        System.out.println("resultFile.getAbsolutePath() = " + htmlResultFile.getAbsolutePath());
        assertTrue(htmlResultFile.exists());

    }

}